﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PaggerApplication.Models;
using Alex.Library.Common;

namespace PaggerApplication.Controllers {
    public class HtmlPaggerController : Controller {
        private static List<ProductModel> _products;
        private const int PAGESIZE = 12;

        static HtmlPaggerController () {

            _products = new List<ProductModel> ();
            for ( int i = 0; i < 1000; i++ ) {
                _products.Add ( new ProductModel () {
                    ID = Guid.NewGuid ().ToString (),
                    Name = "Product_" + ( i + 1 ).ToString (),
                    Specification = "Spec_" + RandomHelper.Create ( 0, 1000, i ).ToString (),
                    Price = RandomHelper.Create ( 100, 50000, i ),
                    Count = RandomHelper.Create ( 1000, 10000, i )
                } );
            }
        }

        //
        // GET: /HtmlPagger/PrevNextGet

        public ActionResult PrevNextGet ( int? pageIndex ) {
            int index = pageIndex ?? 0;
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _products.OrderBy ( p => p.ID ).Skip ( index * PAGESIZE ).Take ( PAGESIZE ).ToList (),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return View ( model );
        }

        //
        // GET: /HtmlPagger/PrevNextPost

        public ActionResult PrevNextPost () {
            
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _products.OrderBy ( p => p.ID ).Take ( PAGESIZE ).ToList (),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            ViewBag.SearchModel = new SearchModel ();
            return View ( model );
        }
        
        //
        // POST: /HtmlPagger/PrevNextPost
        [HttpPost]
        public ActionResult PrevNextPost ( int? pageIndex, SearchModel searchModel ) {
            int index = pageIndex ?? 0;
            double minCount = searchModel.MinCount ?? 0;
            double maxCount = searchModel.MaxCount ?? 0;
            IEnumerable<ProductModel> _data = null;
            if ( maxCount > 0 ) {
                _data = _products.Where ( p => p.Count >= minCount && p.Count <= maxCount );
            } else {
                _data = _products;
            }
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _data.OrderBy ( p => p.ID ).Skip ( index * PAGESIZE ).Take ( PAGESIZE ).ToList (),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _data.Count()
            };
            ViewBag.SearchModel = searchModel;
            return View (model);
        }

        //
        // GET: /HtmlPagger/NumericGet

        public ActionResult NumericGet ( int? pageIndex ) {
            int index = pageIndex ?? 0;            
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _products.OrderBy ( p => p.ID ).Skip ( index * PAGESIZE ).Take ( PAGESIZE ).ToList (),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return View ( model );
        }

        //
        // GET: /HtmlPagger/NumericPost

        public ActionResult NumericPost () {

            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _products.OrderBy ( p => p.ID ).Take ( PAGESIZE ).ToList (),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            ViewBag.SearchModel = new SearchModel ();
            return View ( model );
        }

        //
        // POST: /HtmlPagger/NumericPost
        [HttpPost]
        public ActionResult NumericPost ( int? pageIndex, SearchModel searchModel ) {
            int index = pageIndex ?? 0;
            double minCount = searchModel.MinCount ?? 0;
            double maxCount = searchModel.MaxCount ?? 0;
            IEnumerable<ProductModel> _data = null;
            if ( maxCount > 0 ) {
                _data = _products.Where ( p => p.Count >= minCount && p.Count <= maxCount );
            } else {
                _data = _products;
            }
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel> () {
                Data = _data.OrderBy ( p => p.ID ).Skip ( index * PAGESIZE ).Take ( PAGESIZE ).ToList (),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _data.Count ()
            };
            ViewBag.SearchModel = searchModel;
            return View ( model );
        }
    }
}
